package cn.com.cpic.insurance.rulecheck.web;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.cpic.insurance.rulecheck.entity.RuleConsumer;
import cn.com.cpic.insurance.rulecheck.service.RuleConsumerService;

@Results({ @Result(name = CrudActionSupport.RELOAD, location = "rule-consumer.action", type = "redirect", params = {
		"ruleDefId", "${ruleDefId}" }) })
public class RuleConsumerAction extends CrudActionSupport<RuleConsumer> {

	private static final long serialVersionUID = -5086505209066238266L;

	private RuleConsumerService service;

	private Long id;

	private Long ruleDefId;

	private RuleConsumer entity;

	private List<RuleConsumer> items;

	public String execute() {
		return list();
	}

	public String list() {
		items = service.getRuleConsumerList(ruleDefId);
		return SUCCESS;
	}

	public String input() {
		prepareModel();
		return INPUT;
	}

	public String save() {
		service.saveRuleConsumer(this.entity);
		return RELOAD;
	}

	public String delete() {
		service.deleteRuleConsumer(this.id);
		return RELOAD;
	}

	protected void prepareModel() {
		if (id != null) {
			this.entity = service.getRuleConsumer(id);
		} else {
			this.entity = new RuleConsumer();
			this.entity.setRuleDefinitionId(this.ruleDefId);
		}
	}

	@Autowired
	public void setService(RuleConsumerService service) {
		this.service = service;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getRuleDefId() {
		return ruleDefId;
	}

	public void setRuleDefId(String ruleDefId) {
		if (StringUtils.isNotBlank(ruleDefId)) {
			this.ruleDefId = Long.parseLong(ruleDefId);
		}
	}

	public List<RuleConsumer> getItems() {
		return items;
	}

	public RuleConsumer getModel() {
		return entity;
	}
}
